﻿using common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI
{
    public partial class FrmMain : Form
    {
        public int X { get; set; }
        public int Y { get; set; }
        public bool odd = false;
        public FrmMain()
        {
            InitializeComponent();
            systemTime.Text = DateTime.Now.ToString();
            //this.FormBorderStyle = FormBorderStyle.None;
            this.MouseDown +=  new MouseEventHandler( FrmMain_MouseDown);
            this.MouseMove += new MouseEventHandler(FrmMain_MouseMove);
            init();
        }
        private void init()
        {
            
       
           this.WindowState = FormWindowState.Maximized;
            foreach (var i in this.mainPanel.Controls)
            {
                if( i is Form)
                {
                    Form j = i as Form;
                    j.Width = 800;
                    j.Height = 800;
                    j.StartPosition = FormStartPosition.CenterParent;
                }
            }
       
        }
        private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label6_MouseMove(object sender, MouseEventArgs e)
        {
           
        }

        private void label6_MouseEnter(object sender, EventArgs e)
        {
            label6.Cursor = Cursors.Default;
        }

        private void label6_MouseLeave(object sender, EventArgs e)
        {
            label6.Cursor = Cursors.Default;
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标位置，相对于屏幕
                Point mouseSet = Control.MousePosition;
              // MessageBox.Show(X + "   " + mouseSet.X + "  " + Y + "  " +  mouseSet.Y );
                //偏移量，将值mouseSet 偏移 -X,-Y
                mouseSet.Offset(-X, -Y);
                //MessageBox.Show(mouseSet.X + "  " + mouseSet.Y);
                this.Location = mouseSet;
                
            }

        }

        private void FrmMain_Click(object sender, EventArgs e)
        {
            MessageBox.Show(X + "  " + Y + "");
        }

        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
 
          
        }

        private void topPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //相对于窗体的位置
                X = e.X;
                Y = e.Y;
                
            }
        }

        private void topPanel_MouseMove(object sender, MouseEventArgs e)
        {
            FrmMain_MouseMove(sender, e);
        }

        private void topPanel_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(odd == false)
            {
                //大屏显示
                this.WindowState = FormWindowState.Normal;
              
                odd = true;
            }
            else
            {
                this.WindowState =  FormWindowState.Maximized;
            
                odd = false;
            }

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void userName_Click(object sender, EventArgs e)
        {

        }

        private void FrmMain_Load(object sender, EventArgs e)
        {


        }

        private void FrmMain_SizeChanged(object sender, EventArgs e)
        {
            // clientSize 窗体大小
            int x = (this.ClientSize.Width - label3.Width) / 2 ;
            int y = (this.topPanel.Height - label3.Height) / 2 ;
    
            label3.Location = new Point()
            {
                X = x,
                Y = y

            };
            foreach (Control j in this.mainPanel.Controls)
            {
                if (j is Form)
                {
                    Form k = j as Form;
                    k.Width = this.Width;
                    k.Height = mainPanel.Height;

                }
            }

        }

        private void OpenWindow(Form view)
        {
            //开新窗口前，关闭其他窗口
            foreach (Control j in this.mainPanel.Controls)
            {
                if (j is Form)
                {
                    Form k = j as Form;
                    k.Close();
                   
                }
            }

                int num = 0;
            //不可为顶层窗体,如果是顶层窗体则不能设置parent属性
            view.TopLevel = false;
            //父元素设置为mian
            view.Parent = this.mainPanel;
            
        
            foreach(Control i in this.mainPanel.Controls)
            {
                //如何这个子控件是窗体
                if(i is Form)
                {
                    if(i.Name == view.Name)
                    {
                        num++;
                        
                    }
                }
            }

            if(num == 1)
            {
                //view.StartPosition = FormStartPosition.CenterScreen;
                view.Width = this.Width;
                view.Height = this.mainPanel.Height;
                
                view.Show();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FrmView frmView = new FrmView();
            OpenWindow(frmView);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenWindow(new FrmParamsSet());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenWindow(new FrmBendLine());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenWindow(new FrmFaultWarning());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenWindow(new DataReport());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OpenWindow(new FrmHardwareEcology());
        }
    }
}
